﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Configuration; 

namespace LOGIN
{
    public partial class Orderdetails : Form
    {
        
        public Orderdetails()
        {
            InitializeComponent();
         
        }

        
        private void button2_Click(object sender, EventArgs e)
        {
            Order.Text = null;
            Medical.Text = null;
            Num.Text = null;
            Guige.Text = null;
            cost.Text = null;
            Pricesum.Text = null;
            PH.Text = null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Orderdetails_Load(object sender, EventArgs e)
        {
            Order.Text = ordersHelper.order;
            if (ordersHelper.state == "未入库")
            {
                checkedListBox1.SetItemChecked(0, true);
            }
            
        }

        private void insert_Click_1(object sender, EventArgs e)
        {
            //if (this.Order.Text.Trim() == "")
            //{
            //    MessageBox.Show("订单不能为空！");
            //    this.Order.Focus();
            //    return;
            //}
            //if (this.comb_Supplyname.ValueMember == "")
            //{
            //    MessageBox.Show("供应商不能为空！");
            //    this.Order.Focus();
            //    return;
            //}

            SqlConnection sqlConnection = new SqlConnection();                                              //声明并实例化SQL连接；
            sqlConnection.ConnectionString =
            ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;                             //配置管理器从配置文件读取连接字符串，并将之赋予SQL连接的连接字符串属性；
            SqlCommand sqlCommand = new SqlCommand();
            SqlCommand sqlCommand1 = new SqlCommand();
            
            sqlCommand.Connection = sqlConnection;
            sqlCommand1.Connection = sqlConnection;
            
            sqlConnection.Open();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =
                "  SELECT TOP 1 OrderID FROM dbo.orders ORDER BY OrderID DESC;"; 
             SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();                                      //调用SQL命令的方法ExecuteReader来执行命令，并获取数据阅读器；
             if (sqlDataReader.Read())                                                                       //若数据阅读器成功读取到下一条记录（首次查询则表示第一条记录）；
             {
                 this.label9.Text = sqlDataReader["OrderID"].ToString();
             }
             sqlDataReader.Close(); 
            
            //sqlCommand1.CommandText =
            //   "SELECT MedicineID,DrugName FROM dbo.medicineData;";

            sqlCommand.CommandText =
                "UPDATE orders"
                + " SET IndPrice=@IndPrice,Num=@Num,TolPrice=@TolPrice,Guige=@Guige,state=@state"
                + " WHERE OrderID=@OrderID;"; 
            sqlCommand.Parameters.AddWithValue("@OrderID", this.label9.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@IndPrice", this.cost.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Num", this.Num.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@Guige", this.Guige.Text.Trim());
            sqlCommand.Parameters.AddWithValue("@TolPrice", this.Pricesum.Text.Trim());
            //sqlCommand.Parameters.AddWithValue("@PNo",);
            sqlCommand.Parameters.AddWithValue("@state", this.checkedListBox1.SelectedIndex);                                                     
            int rowAffected = sqlCommand.ExecuteNonQuery();                                             
            sqlConnection.Close();                                                                     
            if (rowAffected == 1)                                                                      
            {
                MessageBox.Show("添加成功。");                                                        
            }
            else                                                                                      
            {
                MessageBox.Show("添加失败！");                                                       
            }


        }

       

       

       

       
    }
}
